﻿using System.Linq;
using BlogLite.Controllers;
using BlogLite.Models;
using BlogLite.Tests.Fakes;
using Moq;
using BlogLite.Models.Database;

namespace BlogLite.Tests.Controllers
{
    public class HomeControllerTestContext
    {
        protected readonly HomeController controller;
        protected readonly Mock<IBlogLiteRepository> repository;
        protected readonly FakeBlogLiteDatabase fakeDb;

        public HomeControllerTestContext()
        {
            controller = new HomeController();
            repository = new Mock<IBlogLiteRepository>();
            fakeDb = new FakeBlogLiteDatabase();
    
            // Setup Mocks
            repository.Setup(m => m.Posts).Returns(fakeDb.Posts);
            repository.Setup(m => m.GetPostByPostId(It.IsAny<int>())).Returns((int i) => fakeDb.Posts.SingleOrDefault(p => p.PostId == i));
            repository.Setup(m => m.GetPostByPostId(It.Is<int>(i => i == 404))).Returns<Post>(null);

            // Assign the mock to the controller
            controller.BlogLiteRepository = repository.Object;
        }
    }
}
